package com.parking.mgr.common.util;

import com.alibaba.fastjson2.JSONObject;
import com.parking.mgr.common.config.I18NJsonFileConfig;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * @ClassName MessageUtils
 * @Description 获取i18n资源文件
 * @Auther: zhijianpu
 * @Date: 2023/6/13
 */
public class MessageUtils {

    private static MessageSource messageSource;

    public static void setMessageSource(MessageSource source){
        messageSource=source;
    }
    public MessageUtils() {
        super();
        //this.messageSource = messageSource;
    }

    /**
     * 根据消息键和参数 获取消息 委托给spring messageSource
     *
     * @param code 消息键
     * @param args 参数
     * @return 获取国际化翻译值
     */
    public static String message(String code, String defaultMessage, Object... args)
    {
//        MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.getLanguage();
        I18NJsonFileConfig i18NJsonFileConfig = I18NJsonFileConfig.getBean(I18NJsonFileConfig.class);
        String jsonStr = i18NJsonFileConfig.loadJson(lang);
        JSONObject jsonFile = JSONObject.parseObject(jsonStr);
        JSONObject langJson = jsonFile.getJSONObject("data");
        return langJson.getString(code);
    }

}
